/*********************************************************
*  @file    cm_uart_demo.c
*  @brief   ML302 OpenCPU uart APIs example
*  Copyright (c) 2019 China Mobile IOT.
*  All rights reserved.
*  created by XieGangLiang 2019/10/17
********************************************************/
#include "cm_main.h"

#include "atol_all_head.h"

// #include "atol_kernel_time.h"

/*----------------------------------------------------------------------------------------------------
# 函数功能 数据定义
# 参数介绍
----------------------------------------------------------------------------------------------------*/
static int atol_cnt = 0;
static osTimerId atol_timer; 


/*----------------------------------------------------------------------------------------------------
# 函数功能 定时器的中断函数
# 参数介绍
----------------------------------------------------------------------------------------------------*/
static void atol_timerHandler( void * arg )
{
   atol_cnt++;
   cm_printf("atol_timer:%d,%d\n",atol_cnt,(int)pvTimerGetTimerID(arg));
   osTimerStart( atol_timer, 2000 );
}

osTimerDef( atol_timer, atol_timerHandler );


/*----------------------------------------------------------------------------------------------------
# 函数功能 定时器函数
# 参数介绍
----------------------------------------------------------------------------------------------------*/
void atol_kernle_timer_init(void)
{
    atol_timer = osTimerCreate( osTimer(atol_timer), osTimerOnce, (void*)44 );
    osTimerStart( atol_timer, 2000 );
    cm_printf("atol_kernle_timer, atol_kernle_timer_init, atol_timer start ok\n");
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 系统时间
# 参数介绍
----------------------------------------------------------------------------------------------------*/
unsigned int system_time_run(struct BASE_SYSTEM_TIME_STRUCT *time) {
	// 秒增加
	time->Second++;

    
	// 分增加
	if (time->Second >= 60) {
        
		time->Second = 0;
		time->Minute++;
		cm_printf("\r\n-----系统时间，%2d年-%2d月-%2d日，%2d时-%2d分-%2d秒-----\r\n",time->Year,time->Month,time->Date,time->Hour,time->Minute,time->Second);

	}

	// 时增加
	if (time->Minute >= 60) {
		time->Minute = 0;
		time->Hour++;
	}

	// 天增加
	if (time->Hour >= 24) {
		time->Hour = 0;
		time->Date++;
		
		// 判断是否增加星期
		time->Week++;
		if (time->Week >= 8) {
			time->Week = 1;
		}
	}

	// 月增加
	if (time->Month==1 || time->Month==3 || time->Month==5 || time->Month==7 || time->Month==8 || time->Month==10 || time->Month>=12) {
		if (time->Date == 32) {
			time->Date = 1;

			time->Month++;
		}
	} else if (time->Month==4 || time->Month==6 || time->Month==9 || time->Month==11) {
		if (time->Date == 31) {
			time->Date = 1;

			time->Month++;
		}
	} else if (time->Month==2 && time->MoonFlag==1) {
		if (time->Date == 30) {
			time->Date = 1;

			time->Month++;
		}
	} else if (time->Month==2 && time->MoonFlag==0) {
		if (time->Date == 29) {
			time->Date = 1;

			time->Month++;
		}
	} else {}

	// 年增加
	if (time->Month >= 13) {
		// 复位
		time->Month = 1;
		time->Year++;

		// 是否是闰年
		if (time->Year % 4 == 0) {
			time->MoonFlag = 1;
		} else {
			time->MoonFlag = 0;
		}
	}
	
	return 1;
}























